home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / smiley / smilectl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  10.2 KB  |  387 lines

  1. // SmileCtl.cpp : Implementation of the CSmileCtrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "Smile.h"
  5. #include "SmileCtl.h"
  6. #include "SmilePpg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15.  
  16. IMPLEMENT_DYNCREATE(CSmileCtrl, COleControl)
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Message map
  21.  
  22. BEGIN_MESSAGE_MAP(CSmileCtrl, COleControl)
  23.     //{{AFX_MSG_MAP(CSmileCtrl)
  24.     ON_WM_LBUTTONDOWN()
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Dispatch map
  31.  
  32. BEGIN_DISPATCH_MAP(CSmileCtrl, COleControl)
  33.     //{{AFX_DISPATCH_MAP(CSmileCtrl)
  34.     DISP_PROPERTY_NOTIFY(CSmileCtrl, "Sad", m_bSad, OnSadChanged, VT_BOOL)
  35.     DISP_FUNCTION(CSmileCtrl, "Beep", Beep, VT_EMPTY, VTS_NONE)
  36.     DISP_FUNCTION(CSmileCtrl, "Wink", Wink, VT_EMPTY, VTS_BOOL)
  37.     DISP_STOCKPROP_BACKCOLOR()
  38.     DISP_STOCKPROP_FORECOLOR()
  39.     DISP_STOCKPROP_CAPTION()
  40.     DISP_STOCKPROP_BORDERSTYLE()
  41.     //}}AFX_DISPATCH_MAP
  42.     DISP_FUNCTION_ID(CSmileCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  43. END_DISPATCH_MAP()
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Event map
  48.  
  49. BEGIN_EVENT_MAP(CSmileCtrl, COleControl)
  50.     //{{AFX_EVENT_MAP(CSmileCtrl)
  51.     EVENT_CUSTOM("Outside", FireOutside, VTS_NONE)
  52.     EVENT_CUSTOM("Inside", FireInside, VTS_XPOS_PIXELS  VTS_YPOS_PIXELS)
  53.     //}}AFX_EVENT_MAP
  54. END_EVENT_MAP()
  55.  
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // Property pages
  59.  
  60. BEGIN_PROPPAGEIDS(CSmileCtrl, 1)
  61.     PROPPAGEID(CSmilePropPage::guid)
  62. //    PROPPAGEID(CLSID_CColorPropPage)
  63. //    PROPPAGEID(CLSID_CFontPropPage)
  64. END_PROPPAGEIDS(CSmileCtrl)
  65.  
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // Initialize class factory and guid
  69.  
  70. IMPLEMENT_OLECREATE_EX(CSmileCtrl, "SMILE.SmileCtrl.1",
  71.     0xc01fa303, 0xb475, 0x11d1, 0x87, 0xe8, 0x0, 0x60, 0x97, 0xba, 0x8d, 0x75)
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // Type library ID and version
  75.  
  76. IMPLEMENT_OLETYPELIB(CSmileCtrl, _tlid, _wVerMajor, _wVerMinor)
  77.  
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // Interface IDs
  81.  
  82. const IID BASED_CODE IID_DSmile =
  83.         { 0xc01fa301, 0xb475, 0x11d1, 0x87, 0xe8, 0x0, 0x60, 0x97, 0xba, 0x8d, 0x75 };
  84. const IID BASED_CODE IID_DSmileEvents =
  85.         { 0xc01fa302, 0xb475, 0x11d1, 0x87, 0xe8, 0x0, 0x60, 0x97, 0xba, 0x8d, 0x75 };
  86.  
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // Control type information
  90.  
  91. static const DWORD BASED_CODE _dwSmileOleMisc =
  92.     OLEMISC_ACTIVATEWHENVISIBLE |
  93.     OLEMISC_SETCLIENTSITEFIRST |
  94.     OLEMISC_INSIDEOUT |
  95.     OLEMISC_CANTLINKINSIDE |
  96.     OLEMISC_RECOMPOSEONRESIZE;
  97.  
  98. IMPLEMENT_OLECTLTYPE(CSmileCtrl, IDS_Smile, _dwSmileOleMisc)
  99.  
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CSmileCtrl::CSmileCtrlFactory::UpdateRegistry -
  103. // Adds or removes system registry entries for CSmileCtrl
  104.  
  105. BOOL CSmileCtrl::CSmileCtrlFactory::UpdateRegistry(BOOL bRegister)
  106. {
  107.     if (bRegister)
  108.         return AfxOleRegisterControlClass(
  109.             AfxGetInstanceHandle(),
  110.             m_clsid,
  111.             m_lpszProgID,
  112.             IDS_Smile,
  113.             IDB_Smile,
  114.             FALSE,                      //  Not insertable
  115.             _dwSmileOleMisc,
  116.             _tlid,
  117.             _wVerMajor,
  118.             _wVerMinor);
  119.     else
  120.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  121. }
  122.  
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CSmileCtrl::CSmileCtrl - Constructor
  126.  
  127. CSmileCtrl::CSmileCtrl()
  128. {
  129.     InitializeIIDs(&IID_DSmile, &IID_DSmileEvents);
  130.  
  131.     m_bWink = FALSE;
  132. }
  133.  
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CSmileCtrl::~CSmileCtrl - Destructor
  137.  
  138. CSmileCtrl::~CSmileCtrl()
  139. {
  140. }
  141.  
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CSmileCtrl::OnDraw - Drawing function
  145.  
  146. #define X(x) (int)(xLeft + (x)*xScale/100)    // Scaling macros
  147. #define Y(y) (int)(yTop + (y)*yScale/100)    // so scale is 0 - 100
  148. #define CX(x) (int)((x)*xScale/100)
  149. #define CY(y) (int)((y)*yScale/100)
  150.  
  151. void CSmileCtrl::OnDraw(
  152.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  153. {
  154.     long xLeft = rcBounds.left;            // Use with scaling macros
  155.     long yTop = rcBounds.top;    
  156.     long xScale = rcBounds.Width();
  157.     long yScale = rcBounds.Height();    
  158.  
  159.     int iPenWidth = max(CX(5), CY(5)); // Pen width based on control size
  160.     CPen penFore(PS_SOLID, iPenWidth, TranslateColor(GetForeColor()));
  161.  
  162.     CPen penNull(PS_NULL, 0, (COLORREF)0);    // Null pen for drawing filled ellipses
  163.  
  164.     CBrush brushBack;                   // Background brush
  165.     brushBack.CreateSolidBrush(TranslateColor(AmbientBackColor()));  //Stock Property
  166.  
  167.     CBrush brushSmiley;
  168.     brushSmiley.CreateSolidBrush(TranslateColor(GetBackColor()));
  169.     pdc->FillRect(rcBounds, &brushBack);                // Clear background        
  170.  
  171.     CPen* pPenSave = pdc->SelectObject(&penFore);
  172.     CBrush* pBrushSave = pdc->SelectObject(&brushSmiley);
  173.     pdc->Ellipse(X(10), Y(15), X(90), Y(95));            // Head
  174.  
  175. #if defined(_WIN32_WCE)
  176.     if (m_bSad)                                            // Use Sad
  177.     {
  178.         pdc->MoveTo(X(35),Y(80));
  179.         pdc->LineTo(X(45),Y(70));
  180.         pdc->LineTo(X(55),Y(70));
  181.         pdc->LineTo(X(65),Y(80));
  182.     }
  183.     else
  184.     {
  185.         pdc->MoveTo(X(35),Y(70));
  186.         pdc->LineTo(X(45),Y(80));
  187.         pdc->LineTo(X(55),Y(80));
  188.         pdc->LineTo(X(65),Y(70));
  189.     }
  190. #else
  191.     if (m_bSad)                                            // Use Sad
  192.     {                                                    // Custom Property
  193.         pdc->Arc(X(25), Y(70), X(75), Y(140),            // Sad Mouth
  194.                X(65), Y(75), X(35), Y(75));
  195.     } 
  196.     else 
  197.     {
  198.         pdc->Arc(X(25), Y(10), X(75), Y(80),            // Smile mouth
  199.                X(35), Y(70), X(65), Y(70));
  200.     }
  201. #endif
  202.  
  203.     if (m_bWink)                                        // Left Eye
  204.     {
  205.         iPenWidth = max(CX(1), CY(1));
  206.         CPen penThin(PS_SOLID, iPenWidth, RGB(0x00,0x00,0x00));
  207.         CPen* pThick = pdc->SelectObject(&penThin);
  208.  
  209.         pdc->MoveTo(X(57), Y(35));
  210.         pdc->LineTo(X(65), Y(50));
  211.         pdc->MoveTo(X(57), Y(50));
  212.         pdc->LineTo(X(65), Y(35));
  213.         pdc->SelectObject(pThick);
  214.     }
  215.     else
  216.     {
  217.         pdc->Ellipse(X(57), Y(35), X(65), Y(50));
  218.     }
  219.  
  220.     pdc->Ellipse(X(35), Y(35), X(43), Y(50));            // Right eye
  221.     pdc->Ellipse(X(46), Y(43), X(54), Y(58));            // Nose
  222.     
  223.     pdc->SetBkMode(TRANSPARENT);                        // Use ForeColor
  224.     pdc->SetTextColor(TranslateColor(GetForeColor()));    // Stock Property
  225.  
  226.     SelectStockFont(pdc);                                // Use Font 
  227.                                                         // Stock Property
  228.     CRect rect = rcBounds; 
  229.     pdc->DrawText(InternalGetText(), -1, rect,            // Use Caption 
  230.         DT_SINGLELINE | DT_CENTER | DT_TOP);            // Stock Propery
  231.  
  232.     pdc->SelectObject(pBrushSave);
  233.     pdc->SelectObject(pPenSave);
  234. }
  235.  
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238. // CSmileCtrl::DoPropExchange - Persistence support
  239.  
  240. void CSmileCtrl::DoPropExchange(CPropExchange* pPX)
  241. {
  242.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  243.     COleControl::DoPropExchange(pPX);
  244.  
  245.     PX_Bool(pPX, _T("Sad"), m_bSad, FALSE);
  246. }
  247.  
  248.  
  249. /////////////////////////////////////////////////////////////////////////////
  250. // CSmileCtrl::OnResetState - Reset control to default state
  251.  
  252. void CSmileCtrl::OnResetState()
  253. {
  254.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  255. }
  256.  
  257. /////////////////////////////////////////////////////////////////////////////
  258. // CSmileCtrl message handlers
  259.  
  260. void CSmileCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
  261. {
  262.     CRect rcBounds;
  263.     GetClientRect(&rcBounds);
  264.  
  265.     long xLeft = rcBounds.left;    //Required by X() and Y() macros
  266.     long yTop = rcBounds.top;    
  267.     long xScale = rcBounds.Width();
  268.     long yScale = rcBounds.Height();
  269.  
  270.     if (InEllipse(point, X(10), Y(15), X(90), Y(95)))
  271.         FireInside((short)point.x, (short)point.y);
  272.     else
  273.         FireOutside();
  274.     
  275.     COleControl::OnLButtonDown(nFlags, point);
  276. }
  277.  
  278. //Simple Ellipse Hit-Testing
  279. BOOL CSmileCtrl::InEllipse(const CPoint& pt,
  280.     int x1, int y1, int x2, int y2)
  281. {
  282.     // Determine radii                       
  283.     double a = (x2 - x1) / 2;
  284.     double b = (y2 - y1) / 2;
  285.     
  286.     // Determine x, y
  287.     double x = pt.x - (x1 + x2) / 2;
  288.     double y = pt.y - (y1 + y2) / 2;
  289.     
  290.     // Apply ellipse formula
  291.     return ((x * x) / (a * a) + (y * y) / (b * b) <= 1);
  292. }
  293.  
  294. void CSmileCtrl::OnSadChanged() 
  295. {
  296.     InvalidateControl();
  297. }
  298.  
  299. void CSmileCtrl::Beep() 
  300. {
  301.     MessageBeep(0);
  302. }
  303.  
  304. void CSmileCtrl::Wink(BOOL bWink) 
  305. {
  306.     m_bWink = bWink;
  307.     InvalidateControl();
  308. }
  309. /////////////////////////////////////////////////////////////////////////////
  310. // CAboutDlg dialog
  311.  
  312. class CAboutDlg : public CDialog
  313. {
  314. // Construction
  315. public:
  316.     CAboutDlg(CWnd* pParent = NULL);   // standard constructor
  317.  
  318. // Dialog Data
  319.     //{{AFX_DATA(CAboutDlg)
  320.     enum { IDD = IDD_ABOUTBOX_Smile };
  321.         // NOTE: the ClassWizard will add data members here
  322.     //}}AFX_DATA
  323.  
  324.  
  325. // Overrides
  326.     // ClassWizard generated virtual function overrides
  327.     //{{AFX_VIRTUAL(CAboutDlg)
  328.     protected:
  329.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  330.     //}}AFX_VIRTUAL
  331.  
  332. // Implementation
  333. protected:
  334.  
  335.     // Generated message map functions
  336.     //{{AFX_MSG(CAboutDlg)
  337.     virtual BOOL OnInitDialog();
  338.     //}}AFX_MSG
  339.     DECLARE_MESSAGE_MAP()
  340. };
  341. /////////////////////////////////////////////////////////////////////////////
  342. // CAboutDlg dialog
  343.  
  344.  
  345. CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
  346.     : CDialog(CAboutDlg::IDD, pParent)
  347. {
  348.     //{{AFX_DATA_INIT(CAboutDlg)
  349.         // NOTE: the ClassWizard will add member initialization here
  350.     //}}AFX_DATA_INIT
  351. }
  352.  
  353.  
  354. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  355. {
  356.     CDialog::DoDataExchange(pDX);
  357.     //{{AFX_DATA_MAP(CAboutDlg)
  358.         // NOTE: the ClassWizard will add DDX and DDV calls here
  359.     //}}AFX_DATA_MAP
  360. }
  361.  
  362.  
  363. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  364.     //{{AFX_MSG_MAP(CAboutDlg)
  365.     //}}AFX_MSG_MAP
  366. END_MESSAGE_MAP()
  367.  
  368. /////////////////////////////////////////////////////////////////////////////
  369. // CAboutDlg message handlers
  370.  
  371. BOOL CAboutDlg::OnInitDialog() 
  372. {
  373.     CDialog::OnInitDialog();
  374.     CenterWindow();    
  375.     return TRUE;  // return TRUE unless you set the focus to a control
  376.                   // EXCEPTION: OCX Property Pages should return FALSE
  377. }
  378.  
  379. /////////////////////////////////////////////////////////////////////////////
  380. // CSmileCtrl::AboutBox - Display an "About" box to the user
  381.  
  382. void CSmileCtrl::AboutBox()
  383. {
  384.     CAboutDlg about;
  385.     about.DoModal();
  386. }
  387.